//发货
var deliverHtml = ''
deliverHtml += '<div class="form-div"><form class="layui-form" id="fomr">';
deliverHtml +='<div class="layui-form-item"><label class="layui-form-label">物流公司</label><div class="layui-input-block layui-form-select-icon iconfont"><select name="logis_id" lay-ignore class="layui-input logis-type"></select></div></div>';
deliverHtml +='<div class="layui-form-item"><label class="layui-form-label">物流运单</label><div class="layui-input-block"><input type="text" name="logis_no" autocomplete="off" class="layui-input" placeholder="请输入物流运单"></div></div>';
deliverHtml += '<input type="hidden" name="token" value="' + token + '">';
deliverHtml += '<input type="hidden" name="id">';
deliverHtml += '</form></div>';


layui.use('table', function() {
	var table = layui.table;

	table.on('checkbox(table)', function(obj){
		var checkStatus = table.checkStatus('idTest'),data = checkStatus.data;
         if(data.length>0){
         	$(".center-nav a.hb-bnt").removeClass("hov-n").addClass("hov");
         }else{
         	$(".center-nav a.hb-bnt").removeClass("hov").addClass("hov-n");
         }
     });


	var $ = layui.$, active = {
		reload: function() {
			var csLoding=layer.msg('数据加载中...', {
				icon: 16,
				shade: 0.3,
				success: function() {
					var order_id = $('#order_id').val();
					var mid = $('#mid').val();
					var status = $("#status option:selected").val();
					var start = $('#start').val();
					var end = $('#end').val();
		
					//执行重载
					table.reload('idTest', {
						page: {
							curr: 1 //重新从第 1 页开始
						},
						where: {
							key: {
								order_id:order_id,
								mid:mid,
								status:status,
								start: start,
								end: end
							}
						},
						done: function(){
						     layer.close(csLoding)
						}
					});
				}
			})
			
		},
	    RejectFun: function(){
	       var checkStatus = table.checkStatus('idTest'),data = checkStatus.data;
	       if(data.length<=""){
	      	layer.alert('请选择您要操作的订单', {
				icon: 5
			})
	        return false;
	       }
	       var myArray = new Array();
			for(var i = 0; i < data.length; i++) {
				myArray.push(data[i].order_id)
			}
			layer.alert('是否确定驳回订单！', {
				icon: 2,
				btn: ['确定', '关闭'],
				btn1: function() {
					$.ajax({
						type: 'post',
						dataType: 'json',
						url: RejectSrc,
						data: {
							token: token,
							data: myArray
						},
						success: function(data) {
							var code = data.code;
							var msg = data.msg;
							if(code == 1) {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 1
								}, function() {
									location.reload();
								})
							} else {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 5
								})
							}
						}
					})
				}
			})
	    }
	 }
	$('.right-center .right-btn').on('click', function(){
	    var type = $(this).data('type');
	    active[type] ? active[type].call(this) : '';
	});
	$('.right-center #ScreenBnt').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	var csLoding=layer.msg('数据加载中...', {
		icon: 16,
		shade: 0.3,
		success: function() {
			table.render({
				elem: '#centet-cen',
				url: listSrc,
				height: $(".center-tab").height(),
				id: 'idTest',
				response: {
		  			statusCode: 1, //成功的状态码，默认：0
				},
				page:true,
				cols: [
					[ //标题栏
						{
							type: 'checkbox'
						}, {
							field: 'id',
							title: 'ID',
							width: 60
						}, {
							field: 'order_id',
							title: '订单号',
							width: 130
						}, {
							field: 'mid',
							title: '用户',
							width: 130
						}, {
							field: 'amount',
							title: '价格总计',
							width: 130
						}, {
							field: 'chs_status',
							title: '支付状态',
							width: 130
						}, {
							field: 'des',
							title: '订单描述'
						}, {
							field: 'create_time',
							title: '创建时间',
							width: 130
						}, {
							field: 'oper',
							title: '操作',
							width: 200,
							minWidth: 160,
							toolbar: '#barDemo'
						}
	
					]
				],
				limit: 20,
				done: function(){
				    layer.close(csLoding)
				}
			});
		}

	})

	table.on('tool(table)', function(obj) {
		var data = obj.data;
		if(obj.event === 'detail') {
			var order_id = data.order_id;
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: seeSrc,
				data: {
					order_id: order_id
				},
				success: function(data) {
					var code = data.code;
					var msg = data.msg;
					var data = data.data;
					var goodsHtml = '';
					$.each(data.goods, function(index, value) {
						goodsHtml += '<tr><td><img src="' + value.photo + '"/></td><td>' + value.title + '</td><td>' + value.coin_name + '</td><td>' + value.number + '</td><td>' + value.amount + '</td></tr>';

					})
					var goodsTab = '<div class="layui-main layui-tc"><table class="layui-table"><colgroup><col width="100px"><col width="auto"></colgroup><tbody><tr><td>购买帐号</td><td>' + data.mid + '</td></tr><tr><td>订单状态</td><td>' + data.chs_status + '</td></tr><tr><td>支付金额</td><td><i class="f-orange f20">' + data.amount + '</i></td></tr><tr><td>收货地址</td><td>' + data.address + '</td></tr><tr><td>发货时间</td><td>' + data.create_time + '</td></tr><tr><td>商品列表</td><td colspan="4" style="padding: 0;"><table class="layui-table"><colgroup><col width="150"><col width="150"><col width="200"><col></colgroup><thead><tr><th>图片</th><th>名称</th><th>币种</th><th>数量</th><th>总价</th></tr></thead><tbody>' + goodsHtml + '</tbody></table></td></tr></tbody></table></div>';

					var btHtml = '<span class="layui-inline">订单号：' + data.order_id + '</span><span class="layui-inline">下单时间：' + data.create_time + '</span>'

					if(code == 1) {
						layer.open({
							title: btHtml,
							type: 1,
							area: ['60%', '60%'],
							content: goodsTab,
						});
					} else {
						layer.close(layer.index)
						layer.alert(msg, {
							icon: 5
						})
					}

				}
			})
		}else if(obj.event = 'deliver'){
			layer.open({
				type: 1,
				title: "发货",
				area: ['550px', '240px'], //宽高
				maxmin: true,
				resize: true,
				content: deliverHtml,
				btn: ['提交', '关闭'],
				btn1: function() {
					var Adata = $("#fomr").serialize();
					$.ajax({
						type: 'post',
						dataType: 'json',
						url: DeliverSrc,
						data: Adata,
						success: function(data) {
							var code = data.code;
							var msg = data.msg;
							if(code == 1) {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 1
								}, function() {
									location.reload();
								})
							} else {
								layer.alert(msg, {
									icon: 5
								})
							}
						}
					})
				}
			});
		}
	});
})

layui.use('laydate', function() {
	var laydate = layui.laydate;
	//开始时间
	laydate.render({
		elem: '#start'
	});
	//结束时间
	laydate.render({
		elem: '#end'
	});



})


$(function(){
	statusOrderFun();

	$('#screen-form').formEdit(seet);

	$("#Expbnt").click(function() {
		$("#screen-form").attr("action", ExpSrc);
	})

	$("#ScreenBnt").click(function() {
		$("#screen-form").attr("action", '');
	})
})